/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package StudyBuddy;
//package BackupClasses;
//import org.jfree.chart.*;
//import org.jfree.chart.plot.PlotOrientation;
//import org.jfree.data.category.DefaultCategoryDataset;
//import org.jfree.data.general.DefaultPieDataset;
//import org.jfree.data.xy.*;
//import org.jfree.data.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

/**
 *
 * @author Ra
 */
public class Scoring {
    
    
    
    String username = null;
    String buddytoDisplay = "Empty";
    boolean pass = false;        
    Statistics stats;
    String buddyName = new String("");
    int buddyScore;
    
    
    ReadWrite write = new ReadWrite();
//    public Scoring()
//    {
//        
//    }
    public Scoring(PluginCollection temp){
         stats = new Statistics(temp);
    }
  
   
    public void setBuddytoDisplay()
    {
         String str = JOptionPane.showInputDialog(null, "Enter Buddy Name : ", 
                            "Buddy Name", 1);
        buddytoDisplay = str;
        System.out.println("Trimmed budyyy i s  " + buddytoDisplay);
       
    }
    public void checkScore()
    {
        for (int i = 0; i < stats.collection.getSize(); i++){
            
            buddyName = stats.collection.getPluginName(i); 
            buddyScore = stats.getScore(i);
          System.out.println("DISPLAYING " + buddyName);
          System.out.println("Displaying " + buddyScore);
          
          if (buddyScore >= 1)
          {
              writeToFile();
          }
                  
    
        }
    }
    public void getUser()
    {
        LoginInfo login = new LoginInfo();
       username =  login.getUsernameInfo();
       System.out.println("Current user is " + username);
    }
 
  public void writeToFile()
  {
    //getInfo();
    String temp = null;
    String file = null;
    getUser();
    //buddyName = buddyName.trim();
    temp = username + "&" + buddyScore + "&" + buddyName;
    file = buddyName + ".txt";
    write.Write(temp,file);
    write.Write(temp,"HighScores.txt");
  } 
  public void displayHighScore()
  {
      
     Sort sort = new Sort();
     Stack stack = new Stack();
     String[] toArray;
     String usernameHigh = null;
     String scoreHigh = null;
     String buddyHigh = null;
     String subString1 = null;
     int length = 0;
     int indexFirst = -1;
     int indexLast = -1;
      
    stack = write.Read("HighScores");
    toArray = new String[stack.getSize()];
    
    int j = 0; //stack.getSize();
          while(stack.isEmpty() == false)
          {
         
              toArray[j] =(String) stack.pop();
             // System.out.println("Adding .." + toArray[j]);
              
              //System.out.println("At this time j is...." + j);
             // j--;
              j++;
          }//while
    
    sort.sort(toArray);
    
//    j = 0;
//    while((j <= toArray[j].length()) && (toArray[j] != null))
//    {
//         indexFirst =  toArray[j].indexOf("&");
//         usernameHigh = toArray[j].substring(0, indexFirst++);
//         System.out.println("HIGH username " + usernameHigh);
//         
//          indexFirst =  toArray[j].indexOf("&");
//          indexLast = toArray[j].lastIndexOf("&");
//          indexFirst++;
//          scoreHigh = toArray[j].substring(indexFirst, indexLast++);
//          System.out.println("HIGH score " + scoreHigh);
//          length = toArray[j].length();
//          
//          buddyHigh = toArray[j].substring(indexLast, length);
//          System.out.println("HIGH buddy " + buddyHigh);
//          //System.out.println("HIGH ScORES " + );
//          
//  
//        subString1 = scoreHigh + " " + usernameHigh + " " + buddyHigh; 
//        
//        toArray[j] = subString1;
//        
//        j++;
//    }
    
    
    JFrame frame = new JFrame("High Scores List");
    JPanel panel = new JPanel();
   // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    String[] Printable = new String[toArray.length];
    for (int n = 0; n< toArray.length; n++){

        Printable[n] = n + ") Username: " + toArray[n].replaceFirst("&", "    Score: \n").replaceFirst("&", "     Buddy: \n");
    }
    
    JList list = new JList(Printable);
    
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    panel.add(list);
    frame.add(panel);
    frame.setSize(400,400);
    frame.setVisible(true);
      

  }
  
  /*
  public void displayGraph()
  {
      Stack stack = new Stack();
      Stack stack2 = new Stack();
      String[] toArray;
      String[] toArray2;
    //  String nametoSearh = null;
      int indexFirst = -1;
      int indexLast = -1;
      int score = 0;
      String subString1 = "iamEempty";
     // int i = 0;
      int day = 1;
      
      
      stack2 = write.Read(buddytoDisplay);
      
      while(stack2.isEmpty()==false)
      {
          stack.push(stack2.pop());
      }
      
      
      toArray = new String[stack.getSize()];
      toArray2 = new String[stack.getSize()];
      
      getUser();
      int j = 0; //stack.getSize();
          while(stack.isEmpty() == false)
          {
         
              toArray[j] =(String) stack.pop();
              System.out.println("Adding .." + toArray[j]);
              
              System.out.println("At this time j is...." + j);
             // j--;
              j++;
          }//while
      
      int i = 0;
      int k = 0;
      while(i < (toArray.length))
          {
//              System.out.println("I's value " +  i);
//              System.out.println("Printing..." + toArray[i]);
//             
//              System.out.println("Chart Array ..." + toArray[i]);
//          
                  
                 indexFirst =  toArray[i].indexOf("&");
                 //indexLast = toArray[i].lastIndexOf("&");
                 //indexFirst++;
                 subString1 = toArray[i].substring(0, indexFirst++);
                 
                 System.out.println("SSSUUUU is "  + subString1);
                 System.out.println("USERNAME is " + username);
                 if (subString1.equals(username))
                 {
                     
                     System.out.println("ARray " + toArray[i]);
                     toArray2[k] = toArray[i];
                     k++;
                 }
                 i++;
                 
            }//while
      
      
      XYSeries series = new XYSeries("Progress over time");
            i = 0;
               while(i <= (toArray2.length) && (toArray2[i] != null))
          {
              System.out.println("I's value " +  i);
              System.out.println("Printing..." + toArray2[i]);
             
              System.out.println("Chart Array ..." + toArray2[i]);
                 indexFirst =  toArray2[i].indexOf("&");
                 indexLast = toArray2[i].lastIndexOf("&");
                 indexFirst++;
                 subString1 = toArray2[i].substring(indexFirst, indexLast++);
                 score = Integer.parseInt(subString1);
                 series.add(day++,score);
                 
                 System.out.println("Score is ..." + score);
                // howmanytimes++;
                 i++;
         }
            
      String display = null;
      String display2 = null;
      display = " Progress for " + username;
      display2 = "Progress Chart for " + username.toUpperCase();
      XYDataset xyDataset = new XYSeriesCollection(series);
      JFreeChart chart = ChartFactory.createXYLineChart
              (display , " Trys", "Score",
     xyDataset, PlotOrientation.VERTICAL, true, true, false);
      ChartFrame frame1=new ChartFrame(display2,chart);
      frame1.setVisible(true);
      frame1.setSize(700,700);

     }
    */        
      
}




